﻿/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
*/

package com.chasekernan.hxnova.componenteditor.details;

import com.chasekernan.hxnova.componenteditor.DetailsPanel;
import com.chasekernan.hxnova.componenteditor.HullEditor;
import org.aswing.JAdjuster;
import org.aswing.AsWingManager;
import com.chasekernan.hxnova.core.components.Hull;
import com.chasekernan.hxnova.componenteditor.ComponentEditorPanel;
import org.aswing.JButton;
import org.aswing.JTextField;

class HullPanel extends DetailsPanel {
	
    private var armorInput : JAdjuster;
    private var initiativeInput : JAdjuster;
	private var fuelCapacityInput : JAdjuster;
	private var backgroundPictureInput : JTextField;
	private var editSlotsButton : JButton;
	private var editSlotsWindow : HullEditor;
    
    public function new() {
        super();
        
		armorInput = DetailsPanel.createIntAdjuster(1, 10000);
		initiativeInput = DetailsPanel.createIntAdjuster(0, 100);
		fuelCapacityInput = DetailsPanel.createIntAdjuster(0, 100000);
		backgroundPictureInput = new JTextField();
		backgroundPictureInput.setPreferredWidth(100);
		editSlotsButton = new JButton("Edit Slots");
		editSlotsButton.addActionListener(onEditSlots);
		
		
		addRowHaxe(createLeftLabel("Background Picture"), 
				   backgroundPictureInput, null);
		addRowHaxe(createLeftLabel("Base Armor"), armorInput, null);
		addRowHaxe(createLeftLabel("Base Initiative"), initiativeInput, null);
		addRowHaxe(createLeftLabel("Fuel Capacity"), fuelCapacityInput, null);
		addRowHaxe(editSlotsButton, null, null);
		
        allInputs = [armorInput, initiativeInput, fuelCapacityInput, 
				     backgroundPictureInput, editSlotsButton];
    }
	
	private function onEditSlots(?e : Dynamic) {
		if (ComponentEditorPanel.INSTANCE.editedComponent == null) return;
		
		editSlotsWindow = new HullEditor(AsWingManager.getRoot(),
				cast(ComponentEditorPanel.INSTANCE.editedComponent, Hull));
		editSlotsWindow.show();
	}
    
    public override function loadFromComponent() {
        if (ComponentEditorPanel.INSTANCE.editedComponent == null) return;
        var hull = cast(ComponentEditorPanel.INSTANCE.editedComponent, Hull);
        
		armorInput.setValue(hull.baseArmor);
		initiativeInput.setValue(hull.baseInitiative);
		fuelCapacityInput.setValue(hull.fuelCapacity);
		backgroundPictureInput.setText(
				if (hull.backgroundPic != null) hull.backgroundPic else "");
    }
    
    public override function saveToComponent() {
        if (ComponentEditorPanel.INSTANCE.editedComponent == null) return;
        var hull = cast(ComponentEditorPanel.INSTANCE.editedComponent, Hull);
        
		hull.baseArmor = armorInput.getValue();
		hull.baseInitiative = initiativeInput.getValue();
		hull.fuelCapacity = fuelCapacityInput.getValue();
		hull.backgroundPic = backgroundPictureInput.getText();
    }
}